package lib.pacificapp.ex.weixin.impl;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONObject;

import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.SendAuth;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
 
import android.annotation.SuppressLint;
import android.content.Context; 
import android.util.Log;

import lib.pacificapp.app.ActionContainer;
import lib.pacificapp.ex.weixin.WxLogin;
import lib.pacificapp.ex.weixin.model.WxUser;
import lib.pacificapp.model.HttpResp;
import lib.pacificapp.util.HttpUtils;
import lib.pacificapp.util.ICookie;
public class DefaultWxLogin implements WxLogin {
   
	
	
	@Override
	public boolean getUserInfo(Context context,WxUser usr) {
            if(usr==null||usr.getCode().equals("")){
            	return false;
            	
            }
             JSONObject data=getJSON(parseGetToken(
            		      getAppId(context).string(),
            		      getAppSec(context).string(),
            		      usr.getCode()));
             if(data.optString("access_token").equals(""))return false;
		     JSONObject info=getJSON(parseGetInfo(data.optString("access_token"),data.optString("openid")));
		     if(data.optString("openid").equals(""))return false;
		      usr.setOpenId(info.optString("openid"));
		      usr.setUserNick(info.optString("nickname"));
		      usr.setUserPic(info.optString("headimgurl"));
		      
		 return true;
	}
	@Override
	public boolean initApp(Context context,String appId, String appSec) {
		 getAppId(context).string(appId);
		 getAppSec(context).string(appSec);
		 return true;
	}
	private ICookie getAppId(Context activity){
		return new ICookie(activity,this.getClass().getName()+"_appId");
		
	}
	private ICookie getAppSec(Context activity){
		 return new ICookie(activity,this.getClass().getName()+"_appSec");
	 }
	protected JSONObject getJSON(String url){
	   HttpUtils client=HttpUtils.getInstance();
	   HttpResp rep=  client.sendRequest(url,null, "get", "utf-8", null, null);
       JSONObject data=null;
	   try {
			data = new JSONObject(rep.getBody());
		  }catch( Exception e) {
			 e.printStackTrace();
		  }
       return data;
}

private String parseGetToken(String appId,String appSec,String code){
	return    "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appId+"&secret="+appSec+"&code="+code+"&grant_type=authorization_code";
	
}
private String parseGetInfo(String token,String openId){

return "https://api.weixin.qq.com/sns/userinfo?access_token="+token+"&openid="+openId;
}

private   IWXAPI api;
@SuppressLint("SimpleDateFormat")
@Override
public boolean callWxApp(Context context) {
	String appId=getAppId(context).string();
	api = WXAPIFactory.createWXAPI(context,appId, true);
	api.registerApp(appId);
	
   if (!api.isWXAppInstalled()) {
      Log.d("debug","没有安装微信");
      return false;
   } 
   SimpleDateFormat dateFormater = new SimpleDateFormat("ddHHss");
   Date date=new Date(); 
   SendAuth.Req req = new SendAuth.Req();
       req.scope = "snsapi_userinfo";
       req.state = dateFormater.format(date); 
	return api.sendReq(req);
}



}
